<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@include file="/include/public.jspf"%>
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta name="format-detection" content="telephone=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1,user-scalable=no" />
<link rel="stylesheet" type="text/css" href="${path }/css/default/m-base.css" />
<link rel="stylesheet" type="text/css" href="${path }/css/default/m-common.css" />
<link rel="stylesheet" type="text/css" href="${path }/css/default/p-all.css" />
<script type="text/javascript" src="${path }/js/jquery1.10.2.min.js"></script>
<script type="text/javascript" src="${path }/js/lib/zepto.js"></script>
<script type="text/javascript" src="${path }/js/common/base.js"></script>
<script type="text/javascript" src="${path }/js/lib/lib.js"></script>

<title>魔环金融</title>
</head>
<body>
   <div class="ui_title">
        <a class="ui_back" href="javascript:void(0)" onclick="window.history.back();"><i class="ui_icon_back"></i>返回</a>
        投资确认</div>
    <section class="m-main p_deal">
        <div class="inv-header mb10">
            <div class="invh-title">
                100起投，e收赢050-8            </div>
            <div class="f15">
                <p>6个月 / 年化10.50% / 按月付息到期还本</p>
                <p>可投：983,564.11元</p>
            </div>
        </div>
        <div class="ui_bg_box bid_con mb10">
            <div class="bid_kt">可用余额：20.00元<a href="/account/charge">充值</a></div>
            <div class="gray_v1">含红包金额：20元</span></div>
            <div class="put_con">
                <div class="ui_ipt">
                    <input id="JS-money" type="tel" maxlength="9" placeholder="100元起投" class="ui_input">
                </div>
                <div class="put_btn" id="all_bid_btn">全投</div>
            </div>
                        <div class="yq_money gray_v1 mt10">预期收益：--</div>
                    </div>
        <div class="ui_bg_box mb10">
                        <div  id="Js-show_fcode_panel" class="ipt-list clearfix">
                <div class="ipt-wrap"><span class="fl">邀请码</span><div class="coupon_box"><span id="JS-bindCouponStr" class="gray_v1 bindCouponStr">使用邀请码投资，可获额外收益</span><i class="ui_arrow fr"></i></div></div>
            </div>
                                        <div class="ipt-list">
                    <a class="JS-to_contlist" href="${path }/deal/contlist.jsp?dealid=59827&money=100" >合同和协议<i class="ui_arrow fr"></i></a>
                </div>
                    </div>
        <div class="ui-deal-btn">
            <input id="JS-submit" type="button" value="同意合同和协议并投资" class="ui_btn_red">
        </div>
    </section>
        <section class="p_deal_fcode_panel">
        <div class="ui_title">
            <a class="ui_back" href="javascript:void(0)"><i class="ui_icon_back"></i>返回</a>邀请码
        </div>
                <div class="panel_top">
            <div class="ui_ipt">
                <input id="JS-fcode" type="text" maxlength="9" placeholder="邀请码" class="ui_input" value="">
            </div>
        </div>
        <div class="gray_v1 fcode_detail">使用邀请码投资，您与邀请人可获额外收益<br/>绑定邀请码后不可修改</div>
                <div class="ui-deal-btn">
            <input id="JS-fcode_submit" type="button" value="绑定邀请码" class="ui_btn_red" disabled="disabled">
        </div>
    </section>
        <script>
    var productID = "59827";
    var mini = 100;
    var total = 1000000;
    var avaliable = 983564.11;
    var remain = 20;
    var _couponIsFixed = 0;
    var userCoupon = "";
    var couponRate = "";
    var _inBidRequest = false;

    var bidCacheJSON = P2PWAP.cache.get('_PAGE_DEAL_BID_formcache_'+productID);
    bidBackFill(bidCacheJSON);
    function bidBackFill(json) {
        if (typeof json != 'undefined') {
            var bidCacheObj = JSON.parse(json);
            var money = bidCacheObj.money;
            var fcode = bidCacheObj.fcode;
            var fcodeRate = bidCacheObj.fcodeRate;
            if (money != ''){
                $('#JS-money').val(money);
                calcMoney();
                juegeMoney();
            } 
            if (_couponIsFixed) return;
            if (fcode != '') {
                $('#JS-bindCouponStr').html(fcode);
                $('#JS-fcode').val(fcode);
            }
            if (fcodeRate != '') {
                $('.p_deal_fcode_panel .fcode_detail').html('使用邀请码投资可获额外年化' + fcodeRate + '%的返利，返利的计算及发放时间参见具体项目详情页内的规则描述');
            }
        }
    }
    function upateSubmitButton() {
        var disabled = _inBidRequest || $('#JS-money').val() == '';
        if (disabled) {
            $('#JS-submit').attr('disabled', 'disabled');
        } else {
            $('#JS-submit').removeAttr('disabled');
        }
    }
    var _upateSubmitButtonCount = 10;
    var _upateSubmitButtonTimer = setInterval(function(){
        upateSubmitButton();
        _upateSubmitButtonCount--;
        if (_upateSubmitButtonCount < 0) {
            clearInterval(_upateSubmitButtonTimer);
        }
    }, 300);
    $('#all_bid_btn').click(function(){
        $('#JS-money').val(Math.min(avaliable, remain));
        upateSubmitButton();
        juegeMoney();
        calcMoney();
        bidCache();
    });
    $('#JS-money').bind('input',function(){
        upateSubmitButton();
        juegeMoney();
        calcMoney();
        bidCache();
    });
    $("#JS-submit").bind("click", function(){
        if (_inBidRequest) return;
        var msg = juegeMoney();
        if (msg != true) {
            P2PWAP.ui.toast(msg);
            return;
        }
        var data = {};
        data['dealid'] = productID;
        data['money'] = $("#JS-money").val() * 1;
        data['coupon'] = userCoupon;
        _inBidRequest = true;
        upateSubmitButton();
        P2PWAP.util.request('/deal/ajax_dobid',
            function(rawData){
                _inBidRequest = false;
                upateSubmitButton();
                P2PWAP.cache.del('_PAGE_DEAL_BID_formcache_'+productID)
                if (rawData.isO2OBonus) {
                    window.location.href = '/o2o/picklist?action=4&deal_load_id=' + rawData.load_id;
                } else {
                    window.location.href = '/deal/bidsuc';
                }
            },
            function(errorMsg){
                _inBidRequest = false;
                upateSubmitButton();
                P2PWAP.ui.toast(errorMsg);
            },
            'get', data);
    });

    function juegeMoney(){
        var money = $("#JS-money").val() * 1;
        if (!(money > 0)) {
            return "请输入正确的金额";
        } else if (money < mini){
            return "起投金额为" + mini + "元";
        } else if (money > avaliable){
            return "该标剩余" + avaliable + "元";
        } else if (money > remain){
            return "可用余额为" + remain + "元";
        } else {
            changeContraLink();
            return true;    
        }
    }

    function calcMoney(){
                var money = $("#JS-money").val() * 1;
        var per = 5.25;
        $('.yq_money').html('预期收益：' + ((per * money)/100).toFixed(2) + '元');
    }
    function changeContraLink(){
                
        var money = $("#JS-money").val() * 1;
        if (!(money > 0)) return;
        //合同
        $('.JS-to_contlist').attr('href','/deal/contlist?dealid='+ productID + '&money='+ money);
    }

    function bidCache() {
        var _bidBackFillDate = {
            'fcode': userCoupon,
            'money': $("#JS-money").val(),
            'fcodeRate': couponRate
        }
        P2PWAP.cache.set('_PAGE_DEAL_BID_formcache_'+productID,JSON.stringify(_bidBackFillDate));
    }
    //邀请码面板
    var _fcodePanelIsShow = false;
    var _inFcodeRequest = false;
    $('#Js-show_fcode_panel').click(function(){
        if (_fcodePanelIsShow) return;
        upateFcodeButton();
        _fcodePanelIsShow = true;
        document.body.scrollTop = 0;
        $('.p_deal_fcode_panel').addClass('p_show');
    });
    $('.p_deal_fcode_panel .ui_back').click(function(){
        closeFcodePanel();
    });
    function closeFcodePanel() {
        $('.p_deal_fcode_panel').removeClass('p_show');
        setTimeout(function(){
            _fcodePanelIsShow = false;
        },600);
    }
    function upateFcodeButton() {
        var disabled = _inFcodeRequest || $('#JS-fcode').val() == '';
        if (disabled) {
            $('#JS-fcode_submit').attr('disabled', 'disabled');
        } else {
            $('#JS-fcode_submit').removeAttr('disabled');
        }
    }
    $('#JS-fcode').bind('input',function(){
        upateFcodeButton();
    });
    $('#JS-fcode_submit').bind('click',function(){
        if (_couponIsFixed) {
            closeFcodePanel();
            return;
        }
        var couponVal = $.trim($('#JS-fcode').val());
        if (!/^[a-zA-Z0-9]+$/.test(couponVal)) {
            upateFcodeButton();
            P2PWAP.ui.toast('邀请码格式错误！');
            return;
        } else {
            if (_inFcodeRequest) return;
            _inFcodeRequest = true;
            upateFcodeButton();
            P2PWAP.util.request('/account/ajax_check_fcode',
                function(rawData){
                    _inFcodeRequest = false;
                    upateFcodeButton();
                    $('#JS-fcode').after('<i class="fcode_right">');
                    $('.p_deal_fcode_panel .fcode_detail').html('使用邀请码投资可获额外年化' + rawData.rebateRatio + '%的返利，返利的计算及发放时间参见具体项目详情页内的规则描述');
                    userCoupon = couponVal;
                    couponRate = rawData.rebateRatio;
                    _couponIsFixed = true;
                    $('#JS-bindCouponStr').html(userCoupon);
                    bidCache();
                  //面板不可用
                    $('#JS-fcode').attr('disabled', 'disabled').val(userCoupon);
                    $('.p_deal_fcode_panel .ui_ipt').addClass('ui_disabled');
                    $('#JS-fcode_submit').val('确定');
                    closeFcodePanel();
                },
                function(errorMsg){
                    _inFcodeRequest = false;
                    upateFcodeButton();
                    P2PWAP.ui.toast(errorMsg);
                },
                'get', {
                    'invite_code': couponVal
                });
        }
    });
        </script>
</body>
</html>